package com.seewintech.framework.service;

import com.seewintech.framework.common.context.UserContext;
import com.seewintech.framework.common.exception.ServiceException;
import com.seewintech.framework.common.login.LoginUtils;
import com.seewintech.framework.entity.Account;
import com.seewintech.framework.exception.AccountExceptionEnum;
import com.seewintech.framework.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * @author by xiaoTian
 * @date 2022/6/15.
 */
@Service
public class UserTokenService {

    @Autowired
    AuthorizationService authorizationService;

    @Autowired
    AccountService accountService;

    @Autowired
    RedisTemplate<String, String> redisTemplate;


    public UserVo login(String name,String password) {
        Account exist = accountService.selectAccountByName(name);
        if (ObjectUtils .isEmpty(exist)){
            throw ServiceException.buildWithExceptionEnum(AccountExceptionEnum.USER_NOT_EXIST);
        }
        // 1.check password
        LoginUtils.checkPassword(password,exist.getPassword(),exist.getSalt());
        // 2.get token
        UserContext context = UserContext.get();
        String token = authorizationService.createToken(exist.getName());
        UserVo vo = new UserVo();
        vo.setToken(token)
                .setName(name)
                .setId(exist.getId());
        redisTemplate.opsForValue().set(vo.getName(),vo.getToken());
        return vo;
    }

    public Integer register(String name, String password) {

        // if registered
        Account exist = accountService.selectAccountByName(name);
        if (!ObjectUtils.isEmpty(exist)){
            throw ServiceException.buildWithExceptionEnum(AccountExceptionEnum.USER_EXIST);
        }
        // 1.create
        Account account = new Account();
        account.setName(name);
        String salt = BCrypt.gensalt();
        account.setSalt(salt);
        account.setPassword(BCrypt.hashpw(password,salt));
        account.setCreateTime(new Date());
        return accountService.insert(account);
        // 2.login
    }
//
//    private UserVo loginOpt(Account memberDTO,String ip){
//        Long uid = memberDTO.getId();
//        Date expireTime = new Date();
//
//        JWTUser jwtUser = new JWTUser().setName(memberDTO.getName()).setExpireTime(expireTime.getTime()).setIp(ip).setUid(uid);
//        String jwttoken = JWTUtils.generateToken(jwtUser, jwtProperties.getPrivateKey());
//
//        // 删除过去的token
//        UserToken delete = new UserToken();
//        delete.setUid(jwtUser.getUid());
//        delete.setDeleted(true);
//        delete.setUpdateTime(new Date());
//        userTokenMapper.updateUserToken(delete);
//        //token入库
//        UserToken userToken = new UserToken();
//        BeanUtils.copyProperties(jwtUser,userToken);
//        userToken.setDeleted(false);
//        userToken.setToken(jwttoken);
//        userToken.setCreateTime(new Date());
//        userToken.setExpireTime(expireTime.getTime());
//        userToken.setUpdateTime(new Date());
//        userTokenMapper.insert(userToken);
//        return jwttoken;
//    }
}
